package com.api.devices.controller;

import com.api.common.core.controller.BaseController;
import com.api.common.core.domain.AjaxResult;
import com.api.common.core.page.TableDataInfo;
import com.api.common.utils.MessageUtils;
import com.api.devices.domain.vo.ElementVo;
import com.api.devices.service.IElementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/element")
public class ElementController extends BaseController {
    @Autowired
    IElementService elementService;

    @PostMapping("/list")
    public TableDataInfo list(@RequestHeader("App-Language") String appLang
            , @RequestBody ElementVo vo) {
        startPageByPost(vo.getPageNum(), vo.getPageSize());
        vo.initControl(getLoginUser().getUser().getUserName(), appLang);
        return getDataTable(elementService.list(vo));
    }

    @PostMapping("/item")
    public AjaxResult item(@RequestHeader("App-Language") String appLang
            , @RequestBody ElementVo vo) {
        vo.initControl(getLoginUser().getUser().getUserName(), appLang);
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("data", elementService.item(vo));
        return ajaxResult;
    }

    @PostMapping("/create")
    public AjaxResult create(@RequestHeader("App-Language") String appLang
            , @RequestBody ElementVo vo) {
        try {
            vo.initControl(getLoginUser().getUser().getUserName(), appLang);
            AjaxResult ajaxResult = AjaxResult.success();
            ajaxResult.put("data", elementService.create(vo));
            ajaxResult.put("message", MessageUtils.message(appLang, "element.add.success"));
            return ajaxResult;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return AjaxResult.error(MessageUtils.message(appLang, "element.add.error"));
        }
    }

    @PostMapping("/modify")
    public AjaxResult modify(@RequestHeader("App-Language") String appLang
            , @RequestBody ElementVo vo) {
        try {
            vo.initControl(getLoginUser().getUser().getUserName(), appLang);
            elementService.modify(vo);
            return AjaxResult.success(MessageUtils.message(appLang, "element.update.success"));
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return AjaxResult.error(MessageUtils.message(appLang, "element.update.error"));
        }
    }

    @PostMapping("/remove")
    public AjaxResult delete(@RequestHeader("App-Language") String appLang
            , @RequestBody ElementVo vo) {
        try {
            vo.initControl(getLoginUser().getUser().getUserName(), appLang);
            elementService.delete(vo);
            return AjaxResult.success(MessageUtils.message(appLang, "element.delete.success"));
        } catch (Exception e) {
            return AjaxResult.error(MessageUtils.message(appLang, "element.delete.error"));
        }
    }

}
